//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Globalization;
using System.Text;
using NopSolutions.NopCommerce.Common.Caching;
using NopSolutions.NopCommerce.Common.Configuration.Settings;
using NopSolutions.NopCommerce.DataAccess.Products.Specs;

namespace NopSolutions.NopCommerce.Common.Products.Specs
{
    /// <summary>
    /// Specification attribute manager
    /// </summary>
    public class SpecificationAttributeManager
    {
        #region Constants
        private const string SPECIFICATIONATTRIBUTE_BY_ID_KEY = "Nop.specificationattributes.id-{0}";
        private const string PRODUCTSPECIFICATIONATTRIBUTE_ALLBYPRODUCTID_KEY = "Nop.productspecificationattribute.allbyproductid-{0}";
        private const string SPECIFICATIONATTRIBUTE_PATTERN_KEY = "Nop.specificationattributes.";
        private const string PRODUCTSPECIFICATIONATTRIBUTE_PATTERN_KEY = "Nop.productspecificationattribute.";
        #endregion

        #region Utilities
        private static SpecificationAttributeCollection DBMapping(DBSpecificationAttributeCollection dbCollection)
        {
            if (dbCollection == null)
                return null;

            SpecificationAttributeCollection collection = new SpecificationAttributeCollection();
            foreach (DBSpecificationAttribute dbItem in dbCollection)
            {
                SpecificationAttribute item = DBMapping(dbItem);
                collection.Add(item);
            }

            return collection;
        }

        private static SpecificationAttribute DBMapping(DBSpecificationAttribute dbItem)
        {
            if (dbItem == null)
                return null;

            SpecificationAttribute item = new SpecificationAttribute();
            item.SpecificationAttributeID = dbItem.SpecificationAttributeID;
            item.Name = dbItem.Name;

            return item;
        }

        private static ProductSpecificationAttributeCollection DBMapping(DBProductSpecificationAttributeCollection dbCollection)
        {
            if (dbCollection == null)
                return null;

            ProductSpecificationAttributeCollection collection = new ProductSpecificationAttributeCollection();
            foreach (DBProductSpecificationAttribute dbItem in dbCollection)
            {
                ProductSpecificationAttribute item = DBMapping(dbItem);
                collection.Add(item);
            }

            return collection;
        }

        private static ProductSpecificationAttribute DBMapping(DBProductSpecificationAttribute dbItem)
        {
            if (dbItem == null)
                return null;

            ProductSpecificationAttribute item = new ProductSpecificationAttribute();
            item.ProductSpecificationAttributeID = dbItem.ProductSpecificationAttributeID;
            item.ProductID = dbItem.ProductID;
            item.SpecificationAttributeID = dbItem.SpecificationAttributeID;
            item.AttributeValue = dbItem.AttributeValue;
            item.DisplayOrder = dbItem.DisplayOrder;

            return item;
        }
        #endregion

        #region Methods
        /// <summary>
        /// Gets a specification attribute
        /// </summary>
        /// <param name="SpecificationAttributeID">The specification attribute identifier</param>
        /// <returns>Specification attribute</returns>
        public static SpecificationAttribute GetSpecificationAttributeByID(int SpecificationAttributeID)
        {
            string key = string.Format(SPECIFICATIONATTRIBUTE_BY_ID_KEY, SpecificationAttributeID);
            object obj2 = NopCache.Get(key);
            if (SpecificationAttributeManager.CacheEnabled && (obj2 != null))
            {
                return (SpecificationAttribute)obj2;
            }

            DBSpecificationAttribute dbItem = DBSpecificationAttributeProvider.Provider.GetSpecificationAttributeByID(SpecificationAttributeID);
            SpecificationAttribute specificationAttribute = DBMapping(dbItem);

            if (SpecificationAttributeManager.CacheEnabled)
            {
                NopCache.Max(key, specificationAttribute);
            }
            return specificationAttribute;
        }

        /// <summary>
        /// Gets specification attribute collection
        /// </summary>
        /// <returns>Specification attribute collection</returns>
        public static SpecificationAttributeCollection GetSpecificationAttributes()
        {
            DBSpecificationAttributeCollection dbCollection = DBSpecificationAttributeProvider.Provider.GetSpecificationAttributes();
            SpecificationAttributeCollection specificationAttributes = DBMapping(dbCollection);
            return specificationAttributes;
        }

        /// <summary>
        /// Deletes a specification attribute
        /// </summary>
        /// <param name="SpecificationAttributeID">The specification attribute identifier</param>
        public static void DeleteSpecificationAttribute(int SpecificationAttributeID)
        {
            DBSpecificationAttributeProvider.Provider.DeleteSpecificationAttribute(SpecificationAttributeID);
            if (SpecificationAttributeManager.CacheEnabled)
            {
                NopCache.RemoveByPattern(SPECIFICATIONATTRIBUTE_PATTERN_KEY);
                NopCache.RemoveByPattern(PRODUCTSPECIFICATIONATTRIBUTE_PATTERN_KEY);
            }
        }

        /// <summary>
        /// Inserts a specification attribute
        /// </summary>
        /// <param name="Name">The name</param>
        /// <returns>Specification attribute</returns>
        public static SpecificationAttribute InsertSpecificationAttribute(string Name)
        {
            DBSpecificationAttribute dbItem = DBSpecificationAttributeProvider.Provider.InsertSpecificationAttribute(Name);
            SpecificationAttribute specificationAttribute = DBMapping(dbItem);

            if (SpecificationAttributeManager.CacheEnabled)
            {
                NopCache.RemoveByPattern(SPECIFICATIONATTRIBUTE_PATTERN_KEY);
                NopCache.RemoveByPattern(PRODUCTSPECIFICATIONATTRIBUTE_PATTERN_KEY);
            }

            return specificationAttribute;
        }

        /// <summary>
        /// Updates the specification attribute
        /// </summary>
        /// <param name="SpecificationAttributeID">The specification attribute identifier</param>
        /// <param name="Name">The name</param>
        /// <returns>Specification attribute</returns>
        public static SpecificationAttribute UpdateSpecificationAttribute(int SpecificationAttributeID, string Name)
        {
            DBSpecificationAttribute dbItem = DBSpecificationAttributeProvider.Provider.UpdateSpecificationAttribute(SpecificationAttributeID, Name);
            SpecificationAttribute specificationAttribute = DBMapping(dbItem);
            if (SpecificationAttributeManager.CacheEnabled)
            {
                NopCache.RemoveByPattern(SPECIFICATIONATTRIBUTE_PATTERN_KEY);
                NopCache.RemoveByPattern(PRODUCTSPECIFICATIONATTRIBUTE_PATTERN_KEY);
            }

            return specificationAttribute;
        }

        /// <summary>
        /// Deletes a product specification attribute mapping
        /// </summary>
        /// <param name="ProductSpecificationAttributeID">Product specification attribute identifier</param>
        public static void DeleteProductSpecificationAttribute(int ProductSpecificationAttributeID)
        {
            DBSpecificationAttributeProvider.Provider.DeleteProductSpecificationAttribute(ProductSpecificationAttributeID);

            if (SpecificationAttributeManager.CacheEnabled)
            {
                NopCache.RemoveByPattern(SPECIFICATIONATTRIBUTE_PATTERN_KEY);
                NopCache.RemoveByPattern(PRODUCTSPECIFICATIONATTRIBUTE_PATTERN_KEY);
            }
        }

        /// <summary>
        /// Gets a product specification attribute mapping collection
        /// </summary>
        /// <param name="ProductID">Product identifier</param>
        /// <returns>Product specification attribute mapping collection</returns>
        public static ProductSpecificationAttributeCollection GetProductSpecificationAttributesByProductID(int ProductID)
        {
            string key = string.Format(PRODUCTSPECIFICATIONATTRIBUTE_ALLBYPRODUCTID_KEY, ProductID);
            object obj2 = NopCache.Get(key);
            if (SpecificationAttributeManager.CacheEnabled && (obj2 != null))
            {
                return (ProductSpecificationAttributeCollection)obj2;
            }

            DBProductSpecificationAttributeCollection dbCollection = DBSpecificationAttributeProvider.Provider.GetProductSpecificationAttributesByProductID(ProductID);
            ProductSpecificationAttributeCollection productSpecificationAttributes = DBMapping(dbCollection);

            if (SpecificationAttributeManager.CacheEnabled)
            {
                NopCache.Max(key, productSpecificationAttributes);
            }
            return productSpecificationAttributes;
        }

        /// <summary>
        /// Gets a product specification attribute mapping 
        /// </summary>
        /// <param name="ProductSpecificationAttributeID">Product specification attribute mapping identifier</param>
        /// <returns>Product specification attribute mapping</returns>
        public static ProductSpecificationAttribute GetProductSpecificationAttributeByID(int ProductSpecificationAttributeID)
        {
            DBProductSpecificationAttribute dbItem = DBSpecificationAttributeProvider.Provider.GetProductSpecificationAttributeByID(ProductSpecificationAttributeID);
            ProductSpecificationAttribute productSpecificationAttribute = DBMapping(dbItem);
            return productSpecificationAttribute;
        }

        /// <summary>
        /// Inserts a product specification attribute mapping
        /// </summary>
        /// <param name="ProductID">Product identifier</param>
        /// <param name="SpecificationAttributeID">Specification attribute identifier</param>
        /// <param name="AttributeValue">An attribute value</param>
        /// <param name="DisplayOrder">The display order</param>
        /// <returns>Product specification attribute mapping</returns>
        public static ProductSpecificationAttribute InsertProductSpecificationAttribute(int ProductID, int SpecificationAttributeID,
           string AttributeValue, int DisplayOrder)
        {
            DBProductSpecificationAttribute dbItem = DBSpecificationAttributeProvider.Provider.InsertProductSpecificationAttribute(ProductID,
                SpecificationAttributeID, AttributeValue, DisplayOrder);
            ProductSpecificationAttribute productSpecificationAttribute = DBMapping(dbItem);
            if (SpecificationAttributeManager.CacheEnabled)
            {
                NopCache.RemoveByPattern(SPECIFICATIONATTRIBUTE_PATTERN_KEY);
                NopCache.RemoveByPattern(PRODUCTSPECIFICATIONATTRIBUTE_PATTERN_KEY);
            }
            return productSpecificationAttribute;
        }

        /// <summary>
        /// Updates the product specification attribute mapping
        /// </summary>
        /// <param name="ProductSpecificationAttributeID">product specification attribute mapping identifier</param>
        /// <param name="ProductID">Product identifier</param>
        /// <param name="SpecificationAttributeID">Specification attribute identifier</param>
        /// <param name="AttributeValue">An attribute value</param>
        /// <param name="DisplayOrder">The display order</param>
        /// <returns>Product specification attribute mapping</returns>
        public static ProductSpecificationAttribute UpdateProductSpecificationAttribute(int ProductSpecificationAttributeID,
            int ProductID, int SpecificationAttributeID, string AttributeValue, int DisplayOrder)
        {
            DBProductSpecificationAttribute dbItem = DBSpecificationAttributeProvider.Provider.UpdateProductSpecificationAttribute(ProductSpecificationAttributeID,
                ProductID, SpecificationAttributeID, AttributeValue, DisplayOrder);
            ProductSpecificationAttribute productSpecificationAttribute = DBMapping(dbItem);
            if (SpecificationAttributeManager.CacheEnabled)
            {
                NopCache.RemoveByPattern(SPECIFICATIONATTRIBUTE_PATTERN_KEY);
                NopCache.RemoveByPattern(PRODUCTSPECIFICATIONATTRIBUTE_PATTERN_KEY);
            }
            return productSpecificationAttribute;
        }
       
        #endregion

        #region Property
        /// <summary>
        /// Gets a value indicating whether cache is enabled
        /// </summary>
        public static bool CacheEnabled
        {
            get
            {
                return SettingManager.GetSettingValueBoolean("Cache.SpecificationAttributeManager.CacheEnabled");
            }
        }
        #endregion
    }
}
